/**
 * plupload插件的封装，依赖于plupload.js
 */
function extPluploadExcel(webPath, obj) {
	var uploadTips = -1;
	var setting = {
		btn: "pickfiles",
		fieldId: "fileName",
		runtimes : 'gears,html5,flash,silverlight,browserplus',
		browse_button: "pickfiles",
		url : webPath + '/intfc/upload.jhtml',
		flash_swf_url : webPath + '/plugin/plupload158/plupload.flash.swf',
		silverlight_xap_url : webPath + '/plugin/plupload158/plupload.silverlight.xap',
		unique_names : true,
		multi_selection : false,
		max_file_size : '10mb',
		chunk_size : '100kb',
		filters : [ 
		    {title : "excel files",extensions : "xls,xlsx"}
		]
	};
	
	obj = $.extend(setting,obj);
	obj.browse_button = obj.btn;
	
	var uploader = new plupload.Uploader(obj);

	uploader.bind("Init",function(up,params){
		//$("#"+obj.btn+"_pre").hide();
		$("#" + obj.btn).show().css("cursor", "point");
	});
	
	//选择文件后调用
	uploader.bind("FilesAdded",function(up, files) {
		plupload.each(files, function(file) {
			//$("#"+obj.btn).siblings(".progressBar").remove();
			//$("#"+obj.btn).after("<span id='pro_" + file.id + "' class='progressBar'></span>");
			//$("#"+obj.btn).remove();
			//$("#pro_" + file.id).progressBar();
		});
	});
	
	//上传进度条
	uploader.bind("UploadProgress",function(up, file) {
		if(!isNaN(file.percent)){
			//var cp = $.trim($("#pro_"+file.id+"_pbText").text().replace("%",""));
			//if(isNaN(cp)||parseInt(cp)<=parseInt(file.percent)){
			//$("#pro_" + file.id).progressBar(file.percent);
			layer.close(uploadTips);
			var temptips = layer.tips("上传中..." + file.percent + "%", "#" + obj.btn, {guide: 0, more: true});
			if (temptips > uploadTips) {
				uploadTips = temptips;
			}
			//}
		}
	});
	
	//某个文件开始上传
	uploader.bind("UploadFile",function(up, file) {

	});
	
	//某个文件上传完成
	uploader.bind("FileUploaded",function(up, file, data) {
		if (data) {
			if (data.response) {
				try{
					var res = $.parseJSON(data.response);
					if (res && res.result == "success") {
						try{
							$("#" + obj.fieldId).val(file.target_name);
							if(obj.callback){
								obj.callback.call(window, obj.fieldId, file.target_name);
							}else{
								setFileName(obj.fieldId, file.target_name);
							}
						}catch(e){}
						window.setTimeout(function () {
							layer.close(uploadTips);
							layer.tips("文件上传完成！", "#" + obj.btn, {
								guide: 0,
								time: 3,
								more: true,
								style: ['background-color:#78BA32; color:#fff', '#78BA32']
							});
						}, 1000);
					} else {
						alert("文件上传失败!");
					}
				}catch(e){
					alert("没有上传权限或登录已过期!");
				}
			} else {
				alert("文件上传失败!");
			}
		}
	});
	
	//所有图片上传完成
	uploader.bind("UploadComplete",function(up, files) {
		
	});
	
	//发生错误
	uploader.bind("Error",function(up, err) {
		var i = {
				e100 : '出现通用错误。',
				e200 : '出现Http错误。',
				e300 : '出现IO错误。',
				e500 : '出现控件初始化错误。',
				e600 : '文件大小不能超过' + obj.max_file_size,
				e601 : '文件格式错误!',
				e602 : '文件格式错误!',
				e700 : '文件格式错误!'
			};
		if (i['e' + (0 - err.code)]) {
			if(err.code==-601||err.code==-602||err.code==-700){
				var mess_str = [];
				if(obj.filters){
					for(var i in obj.filters){
						mess_str.push(obj.filters[i].extensions);
					}
				}
				alert("文件格式错误！请上传以下格式的文件："+mess_str.join(","));
			}else{
				alert(i['e' + (0 - err.code)]);
			}
		} else {
			alert("上传文件出现错误!");
		}
	});
	
	//文件队列发生变化时调用
	uploader.bind('QueueChanged', function(up, file,object) {	
		up.start(); 	
	});
	
	uploader.init();
	
	return uploader;
}